Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid

Imports Phoenix.DataAccess
Imports Phoenix.Helpers

Namespace Forms.Area
    Public Class EmployeeArea
        Inherits GridAreaBase

        Public Event EmployeeSelectionChanged(ByVal sender As Object, ByVal args As EventArgs)

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            GridView = EmployeeView
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents GridControlEmployees As DevExpress.XtraGrid.GridControl
        Friend WithEvents EmployeeView As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents EmployeeNameColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents EmployeeIconColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents EmployeeIconRI As DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox
        Friend WithEvents EmployeeSysColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents EmployeeSysRI As DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit
        Friend WithEvents SysColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ActiveColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ActiveRI As DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.GridControlEmployees = New DevExpress.XtraGrid.GridControl
            Me.EmployeeView = New DevExpress.XtraGrid.Views.Grid.GridView
            Me.EmployeeNameColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.EmployeeIconColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.EmployeeIconRI = New DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox
            Me.EmployeeSysColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.SysColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.EmployeeSysRI = New DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit
            Me.ActiveColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.ActiveRI = New DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit
            CType(Me.GridControlEmployees, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.EmployeeView, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.EmployeeIconRI, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.EmployeeSysRI, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ActiveRI, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'GridControlEmployees
            '
            Me.GridControlEmployees.Dock = System.Windows.Forms.DockStyle.Fill
            '
            'GridControlEmployees.EmbeddedNavigator
            '
            Me.GridControlEmployees.EmbeddedNavigator.Name = ""
            Me.GridControlEmployees.Location = New System.Drawing.Point(0, 0)
            Me.GridControlEmployees.MainView = Me.EmployeeView
            Me.GridControlEmployees.Name = "GridControlEmployees"
            Me.GridControlEmployees.RepositoryItems.AddRange(New DevExpress.XtraEditors.Repository.RepositoryItem() {Me.EmployeeIconRI, Me.EmployeeSysRI, Me.ActiveRI})
            Me.GridControlEmployees.Size = New System.Drawing.Size(360, 448)
            Me.GridControlEmployees.Styles.AddReplace("VertLine", New DevExpress.Utils.ViewStyleEx("VertLine", "Grid", "", True, False, False, DevExpress.Utils.HorzAlignment.Near, DevExpress.Utils.VertAlignment.Center, Nothing, System.Drawing.Color.Transparent, System.Drawing.Color.Transparent, System.Drawing.Color.Empty, System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
            Me.GridControlEmployees.Styles.AddReplace("FocusedCell", New DevExpress.Utils.ViewStyleEx("FocusedCell", "Grid", New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte)), CType((((((DevExpress.Utils.StyleOptions.StyleEnabled Or DevExpress.Utils.StyleOptions.UseBackColor) _
                                Or DevExpress.Utils.StyleOptions.UseDrawFocusRect) _
                                Or DevExpress.Utils.StyleOptions.UseFont) _
                                Or DevExpress.Utils.StyleOptions.UseForeColor) _
                                Or DevExpress.Utils.StyleOptions.UseImage), DevExpress.Utils.StyleOptions), System.Drawing.SystemColors.Highlight, System.Drawing.SystemColors.HighlightText, System.Drawing.SystemColors.InactiveCaption, System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
            Me.GridControlEmployees.Styles.AddReplace("SystemEmployeeStyle", New DevExpress.Utils.ViewStyleEx("SystemEmployeeStyle", "", New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(204, Byte)), System.Drawing.SystemColors.Window, System.Drawing.SystemColors.WindowText, System.Drawing.Color.Empty, System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
            Me.GridControlEmployees.TabIndex = 1
            Me.GridControlEmployees.Text = "Employees"
            '
            'EmployeeView
            '
            Me.EmployeeView.Columns.AddRange(New DevExpress.XtraGrid.Columns.GridColumn() {Me.EmployeeNameColumn, Me.EmployeeIconColumn, Me.EmployeeSysColumn, Me.SysColumn, Me.ActiveColumn})
            Me.EmployeeView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None
            Me.EmployeeView.FormatConditions.AddRange(New DevExpress.XtraGrid.StyleFormatCondition() {New DevExpress.XtraGrid.StyleFormatCondition(DevExpress.XtraGrid.FormatConditionEnum.Equal, Nothing, "SystemEmployeeStyle", True, Nothing, Me.EmployeeSysColumn, True)})
            Me.EmployeeView.GridControl = Me.GridControlEmployees
            Me.EmployeeView.Name = "EmployeeView"
            Me.EmployeeView.OptionsBehavior.AllowIncrementalSearch = True
            Me.EmployeeView.OptionsBehavior.ShowEditorOnMouseUp = True
            Me.EmployeeView.OptionsDetail.EnableMasterViewMode = False
            Me.EmployeeView.OptionsSelection.MultiSelect = True
            Me.EmployeeView.OptionsView.ShowGroupPanel = False
            Me.EmployeeView.OptionsView.ShowIndicator = False
            '
            'EmployeeNameColumn
            '
            Me.EmployeeNameColumn.Caption = "Name"
            Me.EmployeeNameColumn.FieldName = "FullName"
            Me.EmployeeNameColumn.Name = "EmployeeNameColumn"
            Me.EmployeeNameColumn.Options = CType((((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.EmployeeNameColumn.VisibleIndex = 1
            Me.EmployeeNameColumn.Width = 338
            '
            'EmployeeIconColumn
            '
            Me.EmployeeIconColumn.ColumnEdit = Me.EmployeeIconRI
            Me.EmployeeIconColumn.Name = "EmployeeIconColumn"
            Me.EmployeeIconColumn.Options = CType(((DevExpress.XtraGrid.Columns.ColumnOptions.ReadOnly Or DevExpress.XtraGrid.Columns.ColumnOptions.FixedWidth) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.EmployeeIconColumn.VisibleIndex = 0
            Me.EmployeeIconColumn.Width = 20
            '
            'EmployeeIconRI
            '
            Me.EmployeeIconRI.AutoHeight = False
            Me.EmployeeIconRI.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.EmployeeIconRI.Items.AddRange(New Object() {New DevExpress.XtraEditors.Controls.ImageComboBoxItem("", Nothing, 4)})
            Me.EmployeeIconRI.Name = "EmployeeIconRI"
            '
            'EmployeeSysColumn
            '
            Me.EmployeeSysColumn.Caption = "System"
            Me.EmployeeSysColumn.FieldName = "Sys"
            Me.EmployeeSysColumn.Name = "EmployeeSysColumn"
            Me.EmployeeSysColumn.Options = CType(((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            '
            'SysColumn
            '
            Me.SysColumn.Caption = "System"
            Me.SysColumn.FieldName = "Sys"
            Me.SysColumn.Name = "SysColumn"
            Me.SysColumn.Options = DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered
            '
            'EmployeeSysRI
            '
            Me.EmployeeSysRI.AutoHeight = False
            Me.EmployeeSysRI.Caption = "System"
            Me.EmployeeSysRI.Name = "EmployeeSysRI"
            '
            'ActiveColumn
            '
            Me.ActiveColumn.Caption = "Active"
            Me.ActiveColumn.ColumnEdit = Me.ActiveRI
            Me.ActiveColumn.FieldName = "IsAttendant"
            Me.ActiveColumn.Name = "ActiveColumn"
            '
            'ActiveRI
            '
            Me.ActiveRI.AutoHeight = False
            Me.ActiveRI.Name = "ActiveRI"
            '
            'EmployeeArea
            '
            Me.Controls.Add(Me.GridControlEmployees)
            Me.Name = "EmployeeArea"
            CType(Me.GridControlEmployees, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.EmployeeView, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.EmployeeIconRI, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.EmployeeSysRI, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ActiveRI, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Event Handlers "
        Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            If DesignMode Then Return

            Me.GridControlEmployees.DataSource = New DataView(AppDataLayer.Employees)
            If Not Security.IsAdministrator Then
                SysColumn.ClearFilter()
                ActiveColumn.ClearFilter()
                SysColumn.FilterInfo = New ColumnFilterInfo("[Sys] = False", "Non-System Employees")
                ActiveColumn.FilterInfo = New ColumnFilterInfo("[IsAttendant] = True", "Active Employees")
            Else
                SysColumn.ClearFilter()
                ActiveColumn.ClearFilter()
            End If
        End Sub

        Private Sub EmployeeView_SelectionChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.SelectionChangedEventArgs) Handles EmployeeView.SelectionChanged
            RaiseEvent EmployeeSelectionChanged(Me, Nothing)
        End Sub

        Private Sub EmployeeView_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles EmployeeView.FocusedRowChanged
            Dim rows() As Integer = EmployeeView.GetSelectedRows
            If Not rows Is Nothing Then
                For Each row As Integer In rows
                    EmployeeView.UnselectRow(row)
                Next
            End If

            EmployeeView.SelectRow(e.FocusedRowHandle)

        End Sub

        Private Sub EmployeeView_CustomDrawCell(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles EmployeeView.CustomDrawCell
            Dim empl As DataSetData.Employee = EmployeeView.GetDataRow(e.RowHandle)

            If Not empl.IsAttendant Then
                e.Style.ForeColor = Color.FromKnownColor(KnownColor.GrayText)
            Else
                e.Style.ForeColor = Color.FromKnownColor(KnownColor.WindowText)
            End If

        End Sub

#End Region

#Region " Area Behaviour "

        Public Overrides ReadOnly Property Icon() As DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox
            Get
                Return EmployeeIconRI
            End Get
        End Property

        Public Overrides ReadOnly Property IconColumn() As DevExpress.XtraGrid.Columns.GridColumn
            Get
                Return EmployeeIconColumn
            End Get
        End Property
#End Region

#Region " Cut/Copy/Paste/Delete "
        Public Overrides Function CanCopy() As Boolean
            Return Security.CanCreateEmployee
        End Function

        Public Overrides Function CanDelete() As Boolean
            Return Security.CanDeleteEmployee
        End Function

        Protected Overrides Function AllowDeleteItem(ByVal obj As Object) As Boolean
            If Not MyBase.AllowDeleteItem(obj) Then Return False
            If obj Is Nothing Then Return False
            Dim e As DataSetData.Employee = CType(obj, DataSetData.Employee)
            If e.Sys Then
                Return False
            Else
                Return True
            End If
        End Function

        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then Return

            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetData.Employee = Helpers.EmployeeHelper.Copy(CType(obj, DataAccess.DataSetData.Employee))
                EmployeeHelper.UpdateEmployee(item, Me)
                EmployeeHelper.CopyEmployeeSchedule(item, obj, Me)
            Next
        End Sub

        Public Overrides Sub Delete(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If

            Helpers.EmployeeHelper.DeleteEmployees(arr, Me)
        End Sub


#End Region

#Region " Area Specific "
        Public Sub ApplyFilter()
            If Not Security.IsAdministrator Then
                SysColumn.FilterInfo = New ColumnFilterInfo(False)
                ActiveColumn.FilterInfo = New ColumnFilterInfo(True)
            Else
                SysColumn.ClearFilter()
                ActiveColumn.ClearFilter()
            End If
        End Sub
#End Region

    End Class
End Namespace